
import AdManager, { Platform } from "../../Platforms/AdManager";
import AdapterManager, { AdaptaterType } from "../../UIFrame/AdapterManager";
import { FormType } from "../../UIFrame/config/SysDefine";
import UIBase from "../../UIFrame/UIBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NativeBanner extends UIBase {

    formType = FormType.TopTips;
    static prefabPath = "prefabs/UIForms/ad/NativeBanner";

    private adData: any;

    onShow(data) {
        this.adData = data;
        this.initUI(data);
    }

    start() {
        AdapterManager.getInstanse.adapatByType(AdaptaterType.Bottom, this.node, 0);
    }

    initUI(_res) {
        this.node.getChildByName('adLittle').getComponent('cc.Label').string = _res.title;
        this.node.getChildByName('adDesc').getComponent('cc.Label').string = _res.desc || _res.source;

        let str = _res.icon;
        cc.loader.load(str, function (err, texture) {
            let spriteFrame = new cc.SpriteFrame(texture);
            this.node.getChildByName('adIcon').getComponent(cc.Sprite).spriteFrame = spriteFrame;
        }.bind(this));

        this.node.getChildByName('adSprite').on(cc.Node.EventType.TOUCH_START, this.nativeBannerClick, this);
        this.node.getChildByName('btn_close').on(cc.Node.EventType.TOUCH_START, this.nativeBannerClose, this);
    }

    nativeBannerClick() {
        AdManager.getInstance().onNativeAdClick(this.adData.adId);
    }

    nativeBannerClose() {
        this.closeUIForm();

        //设置关闭广告 的openidKey
        AdManager.getInstance().adMGBackend.adClose(this.adData.openIdKey);
    }
}
